﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using VO.Logging;

namespace VO.Web
{
    public class Global : System.Web.HttpApplication
    {

        void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            RegisterRoutes(RouteTable.Routes);

        }

        private void RegisterRoutes(RouteCollection routes)
        {

            //configuring routes
            routes.MapPageRoute("",
                "",
                "~/Default.aspx");
            routes.MapPageRoute("",
                "tracks",
                "~/Tracks.aspx");
            routes.MapPageRoute("",
                "mytracks/",
                "~/MyTracks.aspx");
            routes.MapPageRoute("",
                "mydonetracks/",
                "~/MyDoneTracks.aspx");
            routes.MapPageRoute("",
                "about",
                "~/About.aspx");
            routes.MapPageRoute("",
                "editprofile",
                "~/UserProfile.aspx");
            routes.MapPageRoute("",
                "profile/{userId}",
                "~/UserProfilePublic.aspx");
            routes.MapPageRoute("",
                "myclubs",
                "~/OwnedClubs.aspx");
            routes.MapPageRoute("",
                "tracks/{trackId}",
                "~/TrackDetails.aspx");
            routes.MapPageRoute("",
                "donetracks/{doneTrackId}",
                "~/TrackDetails.aspx");
            routes.MapPageRoute("",
                "downloads",
                "~/Downloads.aspx");
            routes.MapPageRoute("",
                "overview",
                "~/Overview.aspx");

            routes.MapPageRoute("",
                "error",
                "~/ErrorPage.aspx");

            routes.MapPageRoute("",
                "login",
                "~/Account/Login.aspx");
            routes.MapPageRoute("",
                "signup",
                "~/Account/Register.aspx");
            routes.MapPageRoute("",
                "changepassword",
                "~/Account/ChangePassword.aspx");
            routes.MapPageRoute("",
                "changepasswordsucces",
                "~/Account/ChangePasswordSuccess.aspx");
            routes.MapPageRoute("",
                "adminusers",
                "~/Account/AdminUsers.aspx");
            


            //routes.Ignore("{resource}.axd/{*pathInfo}"); 
            //routes.Add(new Route("*.aspx", new StopRoutingHandler()));

            //for debugging routes
            //RouteDebug.RouteDebugger.RewriteRoutesForTesting(routes);
        }

        void Application_End(object sender, EventArgs e)
        {
            //  Code that runs on application shutdown

        }

        void Application_Error(object sender, EventArgs e)
        {
            // At this point we have information about the error
            HttpContext ctx = HttpContext.Current;
            Exception exception = null;

            if (ctx != null
                && ctx.Server != null
                && ((exception = ctx.Server.GetLastError()) != null))
            {
                if (exception is SecurityException)
                {
                    ctx.Server.ClearError();
                    Response.Redirect("~/");
                }
                //TODO - treat validation error
                //if (exception is System.Web.HttpRequestValidationException)
                //{
                //    //thrown when a potentially malicious input string is received from the client as part of the request data
                //    ctx.Server.ClearError();
                //    //TODO - treat validation error
                //    //Response.Redirect("~/ValidationErrorPage.aspx");
                //}
                //else
                //{
                string errRef = Guid.NewGuid().ToString().Substring(0, 8);

                // log exception and any base exception if exists
                LoggerManager.Current.Error(typeof(object),
                    new Exception(
                            string.Format("Application Exception (Error Ref. = {0})", errRef),
                             exception));
                //TODO - send errRef to error page

                ctx.Server.ClearError();
                if (!HttpContext.Current.IsDebuggingEnabled)
                    Response.Redirect("~/error");
                //}
            }

        }

        void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started

        }

        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.

        }

    }
}
